import {
  createRouter,
  createWebHistory,
  createWebHashHistory
} from 'vue-router'
const routes = [{
  path: '/',
  name: 'Login',
  component: () =>
    import('../login/login.vue')
},
{
  path: '/home',
  name: 'home',
  component: () =>
    import('../home/index.vue')
}
]
const router = createRouter({
  // history: createWebHistory(),
  history: createWebHashHistory(process.env.BASE_URL),
  routes: routes
})
const whiteList = ['/']
// 导航守卫
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.getItem('token') // 判断是否登录
  // 没有token
  if (whiteList.indexOf(to.path) !== -1) {
    // 在免登录白名单，直接进入
    next()
  } else {
    if (isLogin) {
      next()
    } else {
      next('/') // 跳转到登录页面
    }
  }
})

export default router
